package com.youxue.teachin.update;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.youxue.teachin.BuildConfig;
import com.youxue.teachin.common.utils.PreferencesUtils;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-30 23:05
 * @version: 9.1.0
 */
public class DownloadManagerReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) {
            if (BuildConfig.DEBUG) {
                System.out.println("用户点击了通知");
            }


            // 点击下载进度通知时, 对应的下载ID以数组的方式传递
            long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);

        } else if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            if (BuildConfig.DEBUG) {
                System.out.println("下载完成");
            }

            /*
             * 获取下载完成对应的下载ID, 这里下载完成指的不是下载成功, 下载失败也算是下载完成,
             * 所以接收到下载完成广播后, 还需要根据 id 手动查询对应下载请求的成功与失败.
             */
            long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1L);
            if (BuildConfig.DEBUG) {
                System.out.println("id: " + id);
            }
            String downloadId = PreferencesUtils.getString("downloadId", "");
            if (downloadId.equals(String.valueOf(id))) {
                String downloadFile = PreferencesUtils.getString("downloadFile", "");
                AutoInstall.install(context, downloadFile);
                PreferencesUtils.delete("downloadId");
                PreferencesUtils.delete("downloadFile");
            }
            // 根据获取到的ID，使用上面第3步的方法查询是否下载成功
        }
    }

}

